#!/usr/bin/perl
use strict;
use warnings;
use URI::Escape;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;

### RingCentral FaxOut API documentation availalbe at
### http://service.ringcentral.com/faxoutapi/

my $RC_API_URL = "http://service.ringcentral.com/faxapi.asp";	# RingCentral FaxOut API URL
#my $RC_API_URL = "https://service.ringcentral.com/faxapi.asp";	# use this instead for HTTPS

my $RCUsername = "9995551212";			# RingCentral Fax Username (10-digit RC phone number)
my $RCPassword = "1234567";				# 5 - 10 digit RC password
my $restFax = "9991234567";
my $attachment = ["/path/to/yourFax.pdf"];	# this should be a binary file of a type supported by RingCentral

##### see here for supported file types
####################################################################################################
##### http://help.ringcentral.com/app/answers/detail/a_id/142/~/supported-file-attachments-for-faxes

my $request =	POST "$RC_API_URL", 
				Content_Type => 'form-data',
				Content      =>	[	Username =>"$RCUsername",	# RingCentral Fax Username (10-digit RC phone number)
									Password =>"$RCPassword",	# RingCentral Fax Password
									Recipient=>"$restFax",		# Restaurant Fax Number
									Coverpage=>'None',			# Should always be 'None' or else default will be used
									Resolution=>'Low',			# can be 'Low' or 'High'
									Attachment=> $attachment
									];

my $useragent	= LWP::UserAgent->new();
my $response = $useragent->request( $request );
my $result = $response->content;					# gets the 'string' content of the response

# 0 - Successful
# 1 - Authorization failed
# 2 - Faxing is prohibited for the account
# 3 - No recipients specified
# 4 - No fax data specified
# 5 - Generic error

print "$result \n";